package cn.com.db.service;

import cn.com.Enum.OrderStatus;
import cn.com.db.model.Order;
import cn.com.util.TimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Service
public class OrderService extends BaseService {

    @Transactional
    public void createOrder(Order order){
        orderDao.createOrder(order);
        orderItemDao.createOrderItems(order.getItems());
    }

    public Order getOrderById(String id)
    {
        if (StringUtils.isBlank(id)){
            return null;
        }
        return orderDao.getOrderById(id);
    }
    public List<Order> getOrder(Order order, Timestamp start,Timestamp end, Integer page, Integer count){
        return orderDao.getOrder(order,start,end,(page-1)*count,count);
    }

    public Integer countOrder(Order order,Timestamp start,Timestamp end){
        return orderDao.countOrder(order,start,end);
    }

    public List<Order> getOrder(Order order, Integer page, Integer count){
        return orderDao.getOrder(order,null, null,(page-1)*count,count);
    }

    public Integer countOrder(Order order){
        return orderDao.countOrder(order,null,null);
    }

    public BigDecimal getTurnover(String storeId,String sellerId){
        return orderDao.getTurnover(sellerId,storeId,OrderStatus.COMPLETE.getStatus());
    }

    public BigDecimal getTodayTurnover(String storeId,String sellerId){
        return orderDao.getTodayTurnover(
                sellerId,
                storeId,
                TimeUtil.getTodayZero(),
                TimeUtil.getTodayTwelve(),
                OrderStatus.COMPLETE.getStatus());
    }

    public Integer getDealPeopleNum(String storeId,String sellerId){
        return orderDao.getDealPeopleNum(sellerId,storeId,OrderStatus.COMPLETE.getStatus());
    }

    public Integer getOrderNum(String storeId,String sellerId){
        return orderDao.getOrderNum(sellerId,storeId,OrderStatus.COMPLETE.getStatus());
    }


    @Transactional
    public void updateOrderStatus(String orderId,Integer status){
        orderDao.updateOrderStatus(orderId,status);
    }

    @Transactional
    public void cancelOrder(String id){
        orderDao.cancel(id);
    }
    
    @Transactional
    public List<Order> getOrderByIdList(List<String> list){
        return orderDao.getOrderByIdList(list);
    }


}
